package org.itzixi.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.itzixi.RequestConstant;
import org.itzixi.grace.result.GraceJSONResult;
import org.itzixi.pojo.FriendCircleLiked;
import org.itzixi.pojo.bo.FriendCircleBO;
import org.itzixi.pojo.vo.FriendCircleVO;
import org.itzixi.service.FriendCircleService;
import org.itzixi.utils.PagedGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/friendCircle")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FriendCircleController {
    private final FriendCircleService friendCircleService;
    @PostMapping("/publish")
    public GraceJSONResult publish(@RequestBody FriendCircleBO friendCircleBO, HttpServletRequest request){
        String userId = request.getHeader(RequestConstant.HEADER_USER_ID);
        if (StringUtils.isBlank(userId))
            return GraceJSONResult.error();
        friendCircleBO.setUserId(userId);
        friendCircleBO.setPublishTime(LocalDateTime.now());

        friendCircleService.publish(friendCircleBO);
        return GraceJSONResult.ok();
    }

    @PostMapping("/queryList")
    public GraceJSONResult queryList(String userId, @RequestParam(defaultValue = "1", name = "page") Integer page,
                                     @RequestParam(defaultValue = "10", name = "pageSize") Integer pageSize){
        if (StringUtils.isBlank(userId))
            return GraceJSONResult.error();
        Page<FriendCircleVO> pageInfo = new Page<>(page, pageSize);
        PagedGridResult<FriendCircleVO> pageResult = friendCircleService.findByPage(userId, pageInfo);
        return GraceJSONResult.ok(pageResult);
    }

    @PostMapping("/like")
    public GraceJSONResult like(String friendCircleId, HttpServletRequest request){
        String userId = request.getHeader(RequestConstant.HEADER_USER_ID);
        if (StringUtils.isBlank(userId))
            return GraceJSONResult.error();
        friendCircleService.like(userId, friendCircleId);
        return GraceJSONResult.ok();
    }

    @PostMapping("/unlike")
    public GraceJSONResult unlike(String friendCircleId, HttpServletRequest request){
        String userId = request.getHeader(RequestConstant.HEADER_USER_ID);
        if (StringUtils.isBlank(userId))
            return GraceJSONResult.error();
        friendCircleService.unlike(userId, friendCircleId);
        return GraceJSONResult.ok();
    }

    @PostMapping("/likedFriends")
    public GraceJSONResult likedFriends(String friendCircleId){
        if (StringUtils.isBlank(friendCircleId))
            return GraceJSONResult.error();
        List<FriendCircleLiked> friendCircleLikeds = friendCircleService.queryLikedFriend(List.of(friendCircleId));
        return GraceJSONResult.ok(friendCircleLikeds);
    }

    @PostMapping("/delete")
    public GraceJSONResult delete(String friendCircleId, HttpServletRequest request){
        String userId = request.getHeader(RequestConstant.HEADER_USER_ID);
        if (StringUtils.isBlank(userId))
            return GraceJSONResult.error();
        friendCircleService.delete(friendCircleId, userId);
        return GraceJSONResult.ok();
    }


}
